/*!
\file ContainerVisualizer.cpp
\brief Contains implementation of ContainerVisualizer.
\ingroup GUI
*/

#include"ContainerVisualizer.h" 

ContainerVisualizer::ContainerVisualizer( int id, QWidget *parent ):QFrame( parent ), m_id(id){
	m_layout = new QVBoxLayout(this);
	setLayout( m_layout );
	
	QLabel *label = new QLabel( "Container " + QString().setNum(id), this );
	m_layout->addWidget(label);
	
	m_widget = new QWidget(this);
	m_layout->addWidget( m_widget );
	
	m_layout = new QHBoxLayout( m_widget );
	m_widget->setLayout( m_layout );
	
	setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	m_widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
	setFrameStyle( QFrame::Panel | QFrame::Sunken );
}

void ContainerVisualizer::insert( int i ){
	QWidget *widget = new QWidget( m_widget );
	m_widgets.insert( i, widget );
	m_layout->insertWidget( i, widget );
	
	QVBoxLayout *layout = new QVBoxLayout( widget );
	m_layouts.insert( i, layout );
	widget->setLayout(layout);
	
	m_data.insert( i, QList<QLabel*>() );
}

void ContainerVisualizer::remove( int i ){
	if( i >= m_data.size() )
		return;
	for( int j = 0; j < m_data[i].size(); j++ ){
		m_layouts[i]->removeWidget( m_data[i][j] );
		delete m_data[i][j];
	}
	m_data.removeAt(i);
	
	m_layout->removeWidget( m_widgets[i] );
	m_widgets.removeAt(i);
	m_layouts.removeAt(i);
}

void ContainerVisualizer::setNode( int i, int nodeID, QString label ){
	QList< QLabel* > &one = m_data[i];
	bool found = false;
	for( int j = 0; j < one.size() && !found; j++ ){
		found = one[j]->text().startsWith("Node");
		if( found ){
			if( nodeID >= 0 )
				one[j]->setText( QString("Node ").append(QString().setNum(nodeID)).append(QString(" ")).append(label) );
			else
				one[j]->setText( "Node -1" );
		}
	}
	if(!found && nodeID >= 0){
		QLabel *qlabel = new QLabel( QString("Node ").append(QString().setNum(nodeID)).append(QString(" ")).append(label), m_widgets[i] );
		m_layouts[i]->addWidget(qlabel);
		one.push_back(qlabel);
	}
}

void ContainerVisualizer::setEdge( int i, int from, int to, QString /*label*/ ){
	QList<QLabel*> &one = m_data[i];
	bool found = false;
	for( int j = 0; j < one.size() && !found; j++ ){
		found = one[j]->text().startsWith("Edge");
		if( found ){
			if( from >= 0 && to >= 0 )
				one[j]->setText( QString("Edge ") + QString().setNum(from) + " -> " + QString().setNum(to) );
			else{
// 				one[j]->setText( "" );
				delete one[j];
				one.takeAt(j);
			}
		}
	}
	if(!found && from >= 0 && to >= 0){
		QLabel *label = new QLabel( QString("Edge ") + QString().setNum(from) + " -> " + QString().setNum(to) );
		m_layouts[i]->addWidget(label);
		one.push_back(label);
	}
}

void ContainerVisualizer::setData( int i, QString data ){
	QList<QLabel*> &one = m_data[i];
	bool found = false;
	for( int j = 0; j < one.size() && !found; j++ ){
		found = one[j]->text().startsWith("Data");
		if( found )
			one[j]->setText( QString("Data ") + data );
	}
	if(!found){
		QLabel *label = new QLabel( QString("Data ") + data );
		m_layouts[i]->addWidget(label);
		one.push_back( label );
	}
}

